<template>
  <div class='u-sys_balanceOfProject_add'>
    <el-dialog
      title="收支项目详情"
      :visible.sync="dialogVisible"
      :before-close="close">
      <el-form :inline='true' label-width='120px'>
        <el-form-item label='收支项目名称：'>
          <el-input v-model.trim='name' />
        </el-form-item>
        <el-form-item label='收支类别：'>
          <a-select v-model='type' :options='typeList' />
        </el-form-item>
      </el-form>
      <div class='u-sys_balanceOfProject_add-btn'>
        <el-button type='primary' @click='confirm'>确定</el-button>
        <el-button type='primary' @click='close'>关闭窗口</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { costFdType } from '@/api/costFdType'

export default {
  name: 'u-sys_balanceOfProject_add',

  data() {
    return {
      // typeList: costFdType.filter(v => parseInt(v.value, 10) >= 100),
      typeList: costFdType.filter(v => v.value === '002' || v.value === '003' || v.value === '102'),
      name: '',
      type: '',
      id: '',
      isAdd: true,
      dialogVisible: false
    }
  },

  methods: {

    $show() {
      this.isAdd = true
      this.dialogVisible = true
    },

    $setData({ id, name, type }) {
      this.id = id
      this.name = name
      this.type = type
      this.isAdd = false
      this.dialogVisible = true
    },

    close() {
      this.dialogVisible = false
    },

    async confirm() {
      const aName = this.name
      const aType = this.type
      let msg = ''
      if (!aName) {
        msg = '请输入名称'
      } else if (!aType) {
        msg = '请选择类别'
      }
      if (msg) {
        return this.__notify('警告', msg, 'warning')
      }
      const { code } = await this.$api[this.isAdd ? 'inventCreateCost' : 'inventModifyCost']({
        aFirmId: this.aFirmId,
        aName,
        aType,
        aId: this.id
      })
      if (code === 0) {
        this.__notify('提示', this.isAdd ? '添加成功' : '修改成功')
        this.$emit('update')
        this.dialogVisible = false
        this.name = ''
        this.type = ''
        this.id = ''
      }
    }
  }
}
</script>

<style lang='less'>
.u-sys_balanceOfProject_add {
  &-btn {
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
  .on {
    font-weight: bold;
  }
}
</style>